package cs437;
import com.trolltech.qt.QSignalEmitter.Signal1;
import com.trolltech.qt.QSignalEmitter.Signal4;
import com.trolltech.qt.core.QPoint;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.*;

public class TalentWidget extends QWidget
{

	private String currentFile;
	
	/*
	 * Point Handling Members
	 */
	
    private int pointsLeft;
    private int pointsSpent;
    private int totalPoints;
    public Signal1<Integer> P = new Signal1<Integer>(); 
    public Signal1<Talent> hovering = new Signal1<Talent>();
    TalentTree tree1, tree2, tree3;
    
    public static void main(String[] args) {
        QApplication.initialize(args);

        TalentWidget testTalentWidget = new TalentWidget(null);
        testTalentWidget.show();
        
        QApplication.exec();
    }
    
    public TalentWidget(QWidget parent){
        super(parent);
        this.setStyleSheet("background: transparent; QToolTip { background-color: black; }");
  
    }
    public void setTotalPoints(int x)
    {
    	totalPoints = x;
    	pointsSpent = 0;
    	pointsLeft = totalPoints;
    	

    }
    
    
    private void setUpTrees() 
    {
    
    	
    	if(currentFile == null)
    	{
    		return;
    	}
    	
    	tree1 = new TalentTree(this, currentFile, 1);
        tree1.setGeometry(0, 0, 265, 645);
        tree1.pointSpent.connect(this, "pointChange(int)");
        tree1.setStyleSheet("background: transparent;"); 
        tree1.hovering.connect(this, "catchHover(Talent)");
        tree1.setFrameShape(QFrame.Shape.NoFrame);
        tree1.show();
        QLabel tree1Name = new QLabel(this);
        tree1Name.setGeometry(20, 670, 60, 30);
        tree1Name.setTextFormat(Qt.TextFormat.RichText);
        tree1Name.setText("<h2><font color=\"yellow\">Test</font></h2>");
         
        tree2 = new TalentTree(this, currentFile, 2);
        tree2.setGeometry(252, 0, 265, 645);
        tree2.pointSpent.connect(this, "pointChange(int)");
        tree2.setStyleSheet("background: transparent;");
        tree2.hovering.connect(this, "catchHover(Talent)");
        tree2.show();
         
        tree3 = new TalentTree(this, currentFile, 3);
        tree3.setGeometry(504, 0, 265, 645);
        tree3.pointSpent.connect(this, "pointChange(int)");
        tree3.setStyleSheet("padding: 0px; margin: 0px;");
        tree3.setFrameShape(QFrame.Shape.NoFrame);
        tree3.show();
        tree3.hovering.connect(this, "catchHover(Talent)");
        
    	updateTrees();
        
         
    }    
    public void pointChange(int change)
    {
    	if(change > 0)
    	{
    		pointsSpent++;
    		pointsLeft--;
    		P.emit(1);
    	}
    	if(change < 0)
    	{
    		pointsSpent--;
    		pointsLeft++;
    		P.emit(-1);
    	} 	
    	updateTrees();
    }
    public void load(String file)
    {
    	currentFile = file;
    	setUpTrees();
    }
    public int getPointsSpent()
    {
    	return pointsSpent;
    }
    public int getTotalPoints()
    {
    	return totalPoints;
    }
    public int getPointsLeft()
    {
    	return pointsLeft;
    }
    public void mousePressEvent(QMouseEvent event) {
		
	    if (event.button() == Qt.MouseButton.LeftButton) {
	    		      
	    	System.out.println("Widget:Left Mouse Clicked");
	    	
	    	event.accept();
	       
	    }
	    if (event.button() == Qt.MouseButton.RightButton) {
	    		      
	    	System.out.println("Widget:Right Mouse Clicked");
	    	
	        event.accept();
	                
	    }
	    this.update();	    
	}
    public void updateTrees()
    {
    	int t1, t2, t3;
    	t1 = tree1.getPointsSpent();
    	t2 = tree2.getPointsSpent();
    	t3 = tree3.getPointsSpent();
    	
    	tree1.setMaxPoints(pointsLeft + t1);
    	tree2.setMaxPoints(pointsLeft + t2);
    	tree3.setMaxPoints(pointsLeft + t3);
    }
    public void catchHover(Talent t)
    {
    	hovering.emit(t);
    }
}
